<?php
function archive_main($db, &$view, $translate) {
	global $config;
	if (isset($_GET['year']) && isset($_GET['month']))
		$rs = $db->query("(SELECT 'reader' as type, id, title, timestamp FROM reader WHERE YEAR(FROM_UNIXTIME(timestamp)) = " . $db->quote($_GET['year']) . " AND MONTHNAME(FROM_UNIXTIME(timestamp)) = " . $db->quote($_GET['month']) . ") UNION (SELECT 'news' as type, id, title, UNIX_TIMESTAMP(pubdate) as timestamp FROM news WHERE YEAR(pubdate) = " . $db->quote($_GET['year']) . " AND MONTHNAME(pubdate) = " . $db->quote($_GET['month']) . ") ORDER BY timestamp DESC");
	else
		$rs = $db->query("(SELECT 'reader' as type, id, title, timestamp FROM reader) UNION (SELECT 'news' as type, id, title, UNIX_TIMESTAMP(pubdate) as timestamp FROM news) ORDER BY timestamp DESC");
	while ($row = $rs->fetch()) {
		$dateArray = getdate($row['timestamp']);
		$archiveArray[] = array('year' => $dateArray['year'], 'month' => $dateArray['month'], 'type' => $row['type'], 'id' => $row['id'], 'title' => $row['title']);
	}
	$view->assign('data', $archiveArray);
	$view->display('archive.tpl');
}

/**
 * Generates an archive list by months.
 *
 * @param array $args Arguments to pass to the module in the form of an assoc array.
 */
function insert_gcmsFeedArchives($args) {
	global $db, $translate;
	$archiveArray = array();
	$first = true;
	$rs = $db->query("(SELECT timestamp FROM reader WHERE FROM_UNIXTIME(timestamp) > INTERVAL -6 MONTH + NOW()) UNION (SELECT UNIX_TIMESTAMP(pubdate) as timestamp FROM news WHERE pubdate > INTERVAL -6 MONTH + NOW()) ORDER BY timestamp DESC");
	while ($row = $rs->fetch()) {
		$dateArray = getdate($row['timestamp']);
		$archiveArray[$dateArray['year']][$dateArray['month']] += 1;
	}
	echo "<ul>\n";
	foreach($archiveArray as $year => $monthArray) {
		foreach($monthArray as $month => $count) {
			print "<li"; if ($first == true) { print ' class="first"'; $first = false; } print "><a href=\"index.php?mod=archive&amp;year=$year&amp;month=$month\">$month ($count)</a></li>\n";
		}
	}
	echo "<li><a href=\"index.php?mod=archive\">Older...</a></li>\n";
	echo "</ul>\n";
}
?>
